import { UI_Listen, UI_Layer } from '../../config/ui_def'
import { Sdk_Platform, OPPO_AD_CONFIG } from '../../frame/constants';
cc.Class({
    extends: cc.Component,

    properties: {
        descript_lab: cc.Label,
        skill_layout: cc.Node,
        merge_sp: cc.Sprite,
        merge_btn: cc.Button,
        icon_node: cc.Node,
        title_lab: cc.Label,
        change_node: cc.Node,
        mergeGold: cc.Label
    },

    // LIFE-CYCLE CALLBACKS:
    init(info) {
        if (info) {
            this.info = info
        } else {
            this.info = { id: 2005, descript: '这是一个很厉害的技能', merge: '2001_1#2002_2/2003_1#2004_1', needGold: 200, name: '车车车' }
        }
        let xinfo = { id: this.info.cardId, lv: this.info.level }
        // this.icon_sp.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.cardId}`)
        this.icon_node.getComponent('skillItem').infoInit(xinfo, 0)
        this.descript_lab.string = `${this.info.description}`
        this.title_lab.string = `${this.info.name}`
        // let mergeGrp = this.info.merge.split('#')
        if (this.info.merge.split('/')[1]) {
            this.change_node.active = true
        } else {
            this.change_node.active = false
        }
        this.changePage = 1
        this.goldNeed = 100
        let getCoin = he.local.getJson('coin', 'user')
        if (getCoin) {
            this.haveGold = getCoin
        } else {
            this.haveGold = 0
        }
        this.needGrp = ``
        let mergeGrp = this.info.merge.split('/')[0].split('#')
        this.mergeInit(mergeGrp)
    },


    changeServer() {
        if (this.changePage == 1) {
            let mergeGrp = this.info.merge.split('/')[1].split('#')
            this.mergeInit(mergeGrp)
            this.changePage = 2
        } else if (this.changePage == 2) {
            let mergeGrp = this.info.merge.split('/')[0].split('#')
            this.mergeInit(mergeGrp)
            this.changePage = 1
        }

    },

    mergeGrpInit() {
        let addNum = 4 - this.skill_layout.childrenCount
        let reward_prefab = he.load.get("uimod/skillItem")
        for (let i = 0; i < addNum; i++) {
            let reward_node = cc.instantiate(reward_prefab)
            this.skill_layout.addChild(reward_node)
        }
    },

    mergeInit(grp) {
        this.mergeGrpInit()
        for (let j = 0; j < this.skill_layout.children.length; j++) {
            this.skill_layout.children[j].active = false
        }
        let card = he.local.getJson('card', 'user')
        this.card_team = he.local.getJson('card_team', 'user')
        this.cardGrp = this.card_team.concat(card)
        this.isFull = true
        this.isInTeam = false
        for (let i = 0; i < grp.length; i++) {

            let itemGrp = grp[i].split('_')
            let itemInfo = he.db.getDataByArgs("Item", { id: parseInt(`${itemGrp[0]}${itemGrp[1]}`) })
            if (itemInfo.type != 3) {
                this.mergeGold.string = `${itemGrp[1]}`
                this.goldNeed = parseInt(itemGrp[1])
                continue
            }
            this.skill_layout.children[i].active = true
            //获取card信息后更新item的图片等信息
            // let cardInfo= he.db.getDataByArgs("Card", { cardId: itemGrp[0], level: itemGrp[1] })
            let itemStr = `#${itemGrp[0]}${itemGrp[1]}_1`
            this.needGrp = this.needGrp.concat(itemStr)
            let cardInfo = { id: parseInt(itemInfo.icon), lv: parseInt(itemInfo.rarity) }
            let scriptItem = this.skill_layout.children[i].getComponent('skillItem')
            scriptItem.infoInit(cardInfo, i)
            scriptItem.cantClick()

            let isHave = this.checkIsHave(parseInt(itemInfo.icon), parseInt(itemInfo.rarity))
            if (isHave) {
                scriptItem.haveLightShow()
            } else {
                scriptItem.dontHaveLight()
            }
            this.checkIsInTeam(parseInt(itemInfo.icon), parseInt(itemInfo.rarity))
            if (isHave && this.isFull) {
                this.isFull = true
            } else {
                this.isFull = false
            }
            if (isHave) {
                this.merge_btn.color = cc.Color.WHITE
            } else {
                switch (common.sdk_env) {
                    case Sdk_Platform.VIVO:
                        this.merge_btn.color = cc.Color.WHITE
                        break
                    default:
                        this.merge_btn.color = cc.Color.GRAY
                        break
                }
            }
            //如果背包里有该塔就从暂时表里删除
            for (let z = 0; z < this.cardGrp.length; z++) {
                if (itemGrp[0] == this.cardGrp[z].id && itemGrp[1] == this.cardGrp[z].lv) {
                    //数据删一下   重新存一下     再刷新一下 背包
                    this.cardGrp.splice(z, 1)
                    // console.log(this.bagInfo)
                    break
                }
            }
        }
        //合成按钮状态
        if (!this.isFull || (this.haveGold < this.goldNeed)) {
            switch (common.sdk_env) {
                case Sdk_Platform.VIVO:
                    this.merge_sp.spriteFrame = he.load.get('merge', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_green')
                    break
                case Sdk_Platform.OPPO:
                    this.merge_sp.spriteFrame = he.load.get('merge', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_green')
                    break
                default:
                    this.merge_sp.spriteFrame = he.load.get('merge', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_green')//btn_gray
                    break
            }
            // this.merge_btn.interactable = false
        } else {
            this.merge_sp.spriteFrame = he.load.get('merge', cc.SpriteAtlas, 'plist').getSpriteFrame('btn_green')
            // this.merge_btn.interactable = true
        }
    },
    //在card  和card_team中一起检测是否有该
    checkIsHave(id, lv) {
        let isHave = false
        if (this.cardGrp) {
            for (let i = 0; i < this.cardGrp.length; i++) {
                if (this.cardGrp[i].id == id && this.cardGrp[i].lv == lv) {
                    isHave = true
                    break
                }
            }
        }
        return isHave
    },
    //是否在card_team里
    checkIsInTeam(id, lv) {
        if (this.card_team) {
            for (let i = 0; i < this.card_team.length; i++) {
                if (this.card_team[i].id == id && this.card_team[i].lv == lv) {
                    this.isInTeam = true
                    this.inTeamIndex = this.card_team[i].index
                    break
                }
            }
        }
    },
    //合成
    mergeServer() {
        if (!this.isFull || (this.haveGold < this.goldNeed)) {
            gg.data.checkIcon(`20001_100${this.needGrp}`, 'merge')
            gg.uiManager.addTip('缺少素材')
            return
        }
        let bag = []
        let team = []
        for (let i = 0; i < this.cardGrp.length; i++) {
            if (this.cardGrp[i].index) {
                team.push(this.cardGrp[i])
            } else {
                bag.push(this.cardGrp[i])
            }
        }
        let newTower
        if (this.isInTeam) {
            newTower = { id: this.info.cardId, lv: this.info.level, index: this.inTeamIndex }
            team.push(newTower)
        }
        else {
            newTower = { id: this.info.cardId, lv: this.info.level }
            bag.push(newTower)
        }
        this.haveGold -= this.goldNeed
        he.local.setJson('coin', this.haveGold, 'user')
        he.local.setJson('card', bag, 'user')
        he.local.setJson('card_team', team, 'user')
        gg.eventManager.emit('topBar', UI_Listen.TOP_UPDATE_MONEY)
        gg.eventManager.emit('card', { type: UI_Listen.SKILL_UPDATEPAGE })

        let id = newTower.id * 10 + newTower.lv
        let info = he.db.getData("Item", id)
        info.num = 1
        gg.uiManager.addPage(UI_Layer.Click, {
            name: 'getStoreItemPage',
            cover: true,
            info: info,
            double: true,
        })


    },


});
